package com.vehicle.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.vehicle.bean.Car;
import com.vehicle.bean.Record;
import com.vehicle.bean.Violation;
import com.vehicle.feign.AccountFeign;
import com.vehicle.form.ViolationForm;
import com.vehicle.mapper.CarMapper;
import com.vehicle.mapper.RecordMapper;
import com.vehicle.mapper.ViolationMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vehicle.service.ViolationService;
import com.vehicle.vo.MessageVO;
import com.vehicle.vo.ViolationListVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 校园车辆管理系统
 * @since 2023-05-14
 */
@Service
public class ViolationServiceImpl extends ServiceImpl<ViolationMapper, Violation> implements ViolationService {

    @Autowired
    ViolationMapper violationMapper;

    @Autowired
    CarMapper carMapper;

    @Autowired
    AccountFeign accountFeign;

    @Autowired
    RecordMapper recordMapper;

    @Override
    public Boolean addVolation(ViolationForm violationForm) {
        Violation violation = new Violation();
        BeanUtils.copyProperties(violationForm,violation);
        violation.setProcessed(0);
        int b = violationMapper.insert(violation);
        if(b==1){
            return true;
        }
        return false;
    }

    @Override
    public Boolean updateMessage(Violation violation) {
        UpdateWrapper<Violation> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("vio_id",violation.getVioId());
        updateWrapper.eq("car_id",violation.getCarId());
        updateWrapper.set("message",violation.getMessage());
        updateWrapper.set("detail",violation.getDetail());
       int b = violationMapper.update(null,updateWrapper);
       if(b == 1){
           return true;
       }
        return false;
    }

    @Override
    public Boolean updateProcessed(Integer vioId) {
        Boolean b = violationMapper.updateViolationProcessed(vioId);
        return b;
    }

    @Override
    public ViolationListVO getAllViolation(Integer page, Integer size) {
        Page<Violation> infoPage = new Page<>(page,size);
        Page<Violation> violationPage = violationMapper.selectPage(infoPage,null);
        ViolationListVO violationListVO = new ViolationListVO();
        violationListVO.setSize(violationPage.getSize());
        violationListVO.setTotal(violationPage.getTotal());
        violationListVO.setContent(violationPage.getRecords());
        return violationListVO;
    }

    @Override
    public List<Violation> getLikeViolation(String key) {
        QueryWrapper<Violation> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("message",key);
        List<Violation> violations = violationMapper.selectList(queryWrapper);
        return violations;
    }

    @Override
    public List<Violation> getListByAdmin(Integer id) {
        QueryWrapper<Violation> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("admin_id",id);
        List<Violation> violations = violationMapper.selectList(queryWrapper);
        return violations;
    }

    @Override
    public List<Violation> getListByUserId(Integer userId) {
        int carId = carMapper.getCarIdByUserId(userId);
        QueryWrapper<Violation> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("car_id",carId);
        List<Violation> violations = violationMapper.selectList(queryWrapper);
        return violations;
    }

    @Override
    public MessageVO getMessageByParking(Integer parkingId) {
        QueryWrapper<Record> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parking_id",parkingId);
        queryWrapper.eq("record_state","停车中");
        Record r = recordMapper.selectOne(queryWrapper);
        MessageVO m = new MessageVO();
        m.setParkingId(parkingId);
        m.setUserId(r.getUserId());
       m.setTel(accountFeign.getUserTel(r.getUserId()));
       QueryWrapper<Car> queryWrapper1 = new QueryWrapper<>();
       queryWrapper.eq("user_id",r.getUserId());
      Car c =  carMapper.selectOne(queryWrapper1);
      m.setCarId(c.getCarId());
      m.setPlateNumber(c.getPlateNumber());
        return m;
    }

    @Override
    public Integer getNumber(Integer userId) {
//        根据用户id获取carId
        int carId = carMapper.getCarIdByUserId(userId);
        return violationMapper.getViolationNumber(carId);
    }

    @Override
    public LocalDateTime getTime(Integer userId) {
        int carId = carMapper.getCarIdByUserId(userId);
        QueryWrapper<Violation> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("car_id",carId);
        queryWrapper.orderByAsc("create_time");
        queryWrapper.last("limit 1");
        Violation violation = violationMapper.selectOne(queryWrapper);
        return violation.getCreateTime();
    }
}
